package com.gisuni.sddc.platformManager.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.omg.CORBA.ServiceInformation;

import com.gisuni.sddc.platformManager.dao.DataThemeDao;
import com.gisuni.sddc.platformManager.model.LayerInfo;
import com.gisuni.sddc.platformManager.model.ResourceInfo;
import com.gisuni.sddc.platformManager.model.ResourceRLayer;
import com.gisuni.sddc.platformManager.model.ServerInfo;
import com.gisuni.sddc.platformManager.pageModel.Tree;
import com.gisuni.sddc.util.HttpUtil;

@IocBean
public class DataThemeService  {
	private Log log = Logs.getLog(DataThemeService.class);
    @Inject
    private DataThemeDao  dataThemeDao;
    
	public List<Tree> allTreeNode() {
		List<ResourceInfo> list = dataThemeDao.getAllTreeNode();
		List<Tree> listTrees = new ArrayList<Tree>();
		for(ResourceInfo  resourceInfo : list){
			Tree tree=new Tree();
			tree.setId(resourceInfo.getId());
			tree.setText(resourceInfo.getResourceName());
			tree.setPid(resourceInfo.getParentId());
			listTrees.add(tree);
		}
		return listTrees;
	}
	
	public Object addTreeNode(String text, String parentNodeId){
		/*//查询
		List<ResourceInfo> list = dataThemeDao.getAllTreeNode();
		List<Integer> ids = new ArrayList<Integer>();
		for(ResourceInfo resourceInfo : list){
			ids.add(Integer.parseInt(resourceInfo.getId()));
		}
		//计算最大值
		Integer max = ids.get(0);
		for(Integer id : ids){
			if(id > max){
				max = id;
			}
		}*/
		//为实体赋值
		ResourceInfo resourceInfo = new ResourceInfo();
		//resourceInfo.setId(Integer.toString(max+1));
		resourceInfo.setId(UUID.randomUUID().toString());
		resourceInfo.setParentId(parentNodeId);
		resourceInfo.setResourceName(text);
		resourceInfo.setCreateTime(new Date());
		return dataThemeDao.addTreeNode(resourceInfo);
	}
	
	public void delTreeNode(String id){
		List<ResourceInfo> list = dataThemeDao.findByPid(id);
		if(list.size()>0){
			for(int i=0;i<list.size();i++){
				dataThemeDao.deleteByIds(list.get(i).getId());
			}
		}
		dataThemeDao.deleteByIds(id);
	}

	public void editTreeNode(String id, String text) {
		dataThemeDao.editTreeNode(id,text);
	}

	public String getConditionConfig(String id, String pid) {
		//1.获取layer_index值
		int layerIndex=getLayerIndex(id);
		//2.获取service url
		String mapServerUrl = getDynamicsUrl(pid);
		//3.获取所有的字段信息
		String getAllFieldsUrl=mapServerUrl+"/exts/SDDCMapSOE/GetLayerInfo?LayerIndex="+layerIndex+"&f=pjson";
		return sendGetResp(getAllFieldsUrl);
	}

	public String getFieldValue(String id, String pid,String field) {
		//1.获取layer_index值
		int layerIndex=getLayerIndex(id);
		//2.获取service url
		String mapServerUrl = getDynamicsUrl(pid);
		
		//3.获取某个字段的值的信息
		String getFieldValueUrl=mapServerUrl+"/exts/SDDCMapSOE/GetUniqueValue?LayerIndex="+layerIndex+"&FieldName="+field+"&f=pjson";
		return sendGetResp(getFieldValueUrl);
	}
	public int getLayerIndex(String id){
		//1.获取layer_index值
		LayerInfo layerInfo = dataThemeDao.findLayerInfoByName(id);
		int layerIndex=0;
		if(layerInfo != null){
			layerIndex = layerInfo.getLayerIndex();
		}
		return layerIndex;
	}
	public String getDynamicsUrl(String pid){
		//2.获取service url
		ServerInfo serverInfo = dataThemeDao.findServerInfoByName(pid);
		String mapServerUrl = serverInfo.getDynamicsUrl();
		return mapServerUrl;
	}
	public String sendGetResp(String url){
		//使用java发送请求
		String respString="";
		try {
			respString = HttpUtil.get(url);
			return respString;
		} catch (IOException e) {
			e.printStackTrace();
			return respString;
		}
	}

	public boolean saveExpress(String resourceId, String layerId, String fields,int edittable,int showfllevel,int tileFlag) {
		ResourceRLayer resourceRLayer = new ResourceRLayer();
		resourceRLayer.setResourceId(resourceId);
		resourceRLayer.setLayerId(layerId);
		resourceRLayer.setFields(fields);
		resourceRLayer.setEdittable(edittable);
		resourceRLayer.setChangelevel(showfllevel);
		resourceRLayer.setTileFlag(tileFlag);
		return dataThemeDao.saveExpress(resourceRLayer);
	}

	public ResourceRLayer getDefinedExpress(String id) {
		return dataThemeDao.getDefinedExpressById(id);
	}
}
